package com.xinshujia.config.authentication.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import java.util.Collection;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-16 12:08       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 *     手机号码登录 认证需要的Token  类似springSecurity自带的 UsernamePasswordAuthenticationToken
 */
public class MyPhoneAuthenticationToken extends AbstractAuthenticationToken {
 private final Object principal;
 private Object credentials;

 public MyPhoneAuthenticationToken(Object principal, Object credentials) {
  super(null);
  this.principal = principal;
  this.credentials = credentials;
  this.setAuthenticated(false);
 }

 public MyPhoneAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
  super(authorities);
  this.principal = principal;
  this.credentials = credentials;
  super.setAuthenticated(true);
 }

 public static MyPhoneAuthenticationToken unauthenticated(Object principal, Object credentials) {
  return new MyPhoneAuthenticationToken(principal, credentials);
 }

 public static MyPhoneAuthenticationToken authenticated(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
  return new MyPhoneAuthenticationToken(principal, credentials, authorities);
 }

 public Object getCredentials() {
  return this.credentials;
 }

 public Object getPrincipal() {
  return this.principal;
 }

 public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
  Assert.isTrue(!isAuthenticated, "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
  super.setAuthenticated(false);
 }

 public void eraseCredentials() {
  super.eraseCredentials();
  this.credentials = null;
 }
}
